package gaml.testing;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import gaml.error.GamlException;
import gaml.text.TextNodeBridge;
import gaml.text.YamlParserStore;
import gaml.text.YamlText;
import gaml.text.parser.MappingNodeParser;
import gaml.text.parser.ScalarNodeParser;
import gaml.text.parser.SequenceNodeParser;
import gaml.yaml.YamlNode;
import gaml.yaml.ahchor.YamlAnchorCodeStore;

import org.junit.Test;


public class SequenceNodeParserTest {
	final static String sampleText = "&seqanch [\n	- > seqval1\n	- &mapanch {\n		? > mapkey2\n		: > mapval2\n		? > mapkey1\n		: > mapval1\n	}\n	- > seqval2\n]\n";
	
	YamlAnchorCodeStore anchors = new YamlAnchorCodeStore ();

	@Test
	public void testParseText () throws GamlException {
		YamlText text = new YamlText ().read (sampleText);
		
		YamlParserStore store = new YamlParserStore (new ScalarNodeParser (anchors));
		store.register (new SequenceNodeParser (anchors, store));
		store.register (new MappingNodeParser (anchors, store));

		assertTrue (store.hasParser (text));

		YamlNode node = new TextNodeBridge (text, null).parseText (store).getNode ();

		assertEquals (3, node.getSequenceChildren ().size ());
		
		for (YamlNode key: node.getSequenceChildren ().get (1).getMappingChildren ().keySet ())
			if ("mapkey2".equals (key.getValue ()))
				assertEquals ("mapval2", node.getSequenceChildren ().get (1).getChild (key).getValue ());

		assertEquals ("seqval2", node.getSequenceChildren ().get (2).getValue ());
		
		for (YamlNode elem: node.getSequenceChildren ()) {
			System.out.println (elem.getClass ().getName ());
			if (null != elem.getValue () && (! elem.getValue ().isEmpty ()))
				System.out.println (elem.getValue ());
			else
				for (YamlNode key: elem.getMappingChildren ().keySet ())
					System.out.println (key.getValue () + " :: " + elem.getChild (key).getValue ());
		}
	}
}